from backend.api.connection import POOL
# from connection import POOL   # 单独运行用这个


# 获取词书列表
# 返回
# {'data': [{bid:123,bookname:"CET6",total_num:2333},
#           {bid:123,bookname:"CET4",total_num:2333}
#           ],
#  'ok': 1,
#  'msg':''}

def getBooklist():
    conn = POOL.connection()
    cur = conn.cursor()
    response = {'data': [], 'ok': 0, 'msg': ''}

    sql = ''
    sql = 'SELECT bid,bookname,total_num FROM booklist'
    cur.execute(sql)
    res = cur.fetchall()
    data = []
    for item in res:
        book = {'bid': 0, 'bookname': "", 'total_num': 0}
        book['bid'] = item[0]
        book['bookname'] = item[1]
        book['total_num'] = item[2]
        data.append(book)
    response['data'] = data
    response['ok'] = 1
    return response

# 下载词书
# 返回
# {'data': [{word:"cat",translation:"",sentence:""},
#           {word:"cat",translation:"",sentence:""}
#           ],
#  'total_num':2333,
#  'ok': 1,
#  'msg':''}


def loadBook(bid):
    conn = POOL.connection()
    cur = conn.cursor()
    response = {'data': [], 'total_num': 0, 'ok': 0, 'msg': ''}

    sql = ''
    sql = 'SELECT word,translation,sentence FROM book_%s' % bid
    cur.execute(sql)
    res = cur.fetchall()
    data = []
    for item in res:
        word = {'word': "", 'translation': "", 'sentence': ""}
        word['word'] = item[0]
        word['translation'] = item[1]
        word['sentence'] = item[2]
        data.append(word)
    response['data'] = data
    response['total_num'] = len(data)
    response['ok'] = 1
    return response


if __name__ == "__main__":
    # userSignup("lzy", '66666')
    print(getBooklist())
